//package demo.hms.util;
//
//import com.auth0.jwt.JWT;
//import com.auth0.jwt.algorithms.Algorithm;
//import com.auth0.jwt.interfaces.Claim;
//import com.auth0.jwt.interfaces.DecodedJWT;
//import org.springframework.stereotype.Component;
//
//import java.util.Date;
//import java.util.HashMap;
//import java.util.Map;
//
//@Component
//public class Toekn {
//     String tokens = "token加密";
////    public String getToken(String userId, String userRule, long time){
////        String token = JWT
////                .create()
////                .withClaim("userId", userId)
////                .withClaim("userRule", userRule)
////                .withClaim("time", time)
////                .sign(Algorithm.HMAC256(tokens));
////        return token;
////    }
//
//
////    public Map<String,String> parseToken(String token){
////        Map<String,String> map = new HashMap<>();
////        DecodedJWT decodedjwt = JWT.require(Algorithm.HMAC256(tokens)).build().verify(token);
////        Claim userId = decodedjwt.getClaim("userId");
////        Claim userRule = decodedjwt.getClaim("userRule");
////        Claim time = decodedjwt.getClaim("time");
////        map.put("userId", userId.asString());
////        map.put("userRule", userRule.asString());
////        map.put("time", time.asLong().toString());
////        return map;
////    }
//
//    public  String getToken(String userId,String password,String roleName,Long time){
//        return JWT.create()
//                .withClaim("userId",userId)
//                .withClaim("password",password)
//                .withClaim("roleNam",roleName)
//                .withClaim("time",time)
//                .sign(Algorithm.HMAC256(tokens));
//    }
//
//    public  Map<String,Object> parseToken(String token){
//        Map<String,Object> map = new HashMap<>();
//        DecodedJWT jwt = JWT.require(Algorithm.HMAC256(tokens)).build().verify(token);
//        Claim userId = jwt.getClaim("userId");
//        Claim password = jwt.getClaim("password");
//        Claim roleName = jwt.getClaim("roleNam");
//        Claim time = jwt.getClaim("time");
//        map.put("userId",userId.asString());
//        map.put("password",password.asString());
//        map.put("roleName",roleName.asString());
//        map.put("time",time.asLong());
//        return map;
//    }
//}


package demo.hms.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class Toekn {
    static String tokens = "token加密";
    public static String getToken(String userId, String password, String roleName, Long time){
        return JWT.create()
                .withClaim("userId",userId)
                .withClaim("password",password)
                .withClaim("roleNam",roleName)  // 注意声明字段名拼写为roleNam
                .withClaim("time",time)
                .sign(Algorithm.HMAC256(tokens));
    }

    public static Map<String,Object> parseToken(String token){
        Map<String,Object> map = new HashMap<>();
        // 验证签名并解码JWT
        DecodedJWT jwt = JWT.require(Algorithm.HMAC256(tokens)).build().verify(token);
        // 提取并转换声明字段
        Claim userId = jwt.getClaim("userId");
        Claim password = jwt.getClaim("password");
        Claim roleName = jwt.getClaim("roleNam");  // 注意原始声明字段名
        Claim time = jwt.getClaim("time");
        // 构建返回结果集
        map.put("userId",userId.asString());
        map.put("password",password.asString());
        map.put("roleName",roleName.asString());  // 转换为roleName键输出
        map.put("time",time.asLong());
        return map;
    }
}
